package com.liuyz.convert_;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class 转换输出流 {

    // 写入内容为UTF-8编码到文件
    @Test
    public void convertStreamWrite() {
        String path = "d:\\test.txt";
        OutputStreamWriter outputStreamWriter = null;
        try {
            // 构建转换流，指定输出的编码
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
            outputStreamWriter.write("safafasfa测试中");
            outputStreamWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != outputStreamWriter) {
                try {
                    outputStreamWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
